package com.hixtrip.sample.infra;

        import com.hixtrip.sample.domain.inventory.repository.InventoryRepository;
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.data.redis.core.RedisTemplate;
        import org.springframework.data.redis.core.ValueOperations;
        import org.springframework.stereotype.Component;

/**
 * infra层是domain定义的接口具体的实现
 */
@Component
public class InventoryRepositoryImpl implements InventoryRepository {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;


    @Override
    public Integer query(String skuId) {
        return null;
    }

    @Override
    public Boolean changeInventory(String skuId, Long sellableQuantity, Long withholdingQuantity, Long occupiedQuantity) {
        Integer num = (Integer)redisTemplate.opsForValue().get(skuId);
        if (num > withholdingQuantity){
            redisTemplate.opsForValue().set(skuId,sellableQuantity-withholdingQuantity);
            sellableQuantity -= withholdingQuantity;
            return true;
        }
        return false;
    }
}
